package com.sunday.volunteer.admin.ui.index;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.event.EventBus;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.UIAlertView;
import com.sunday.common.widgets.banner.ConvenientBanner;
import com.sunday.member.base.BaseActivity;
import com.sunday.member.event.ExitApp;
import com.sunday.member.utils.SharePerferenceUtils;
import com.sunday.volunteer.admin.BaseApp;
import com.sunday.volunteer.admin.R;
import com.sunday.volunteer.admin.entity.MyActivities;
import com.sunday.volunteer.admin.event.CancelActivityEvent;
import com.sunday.volunteer.admin.http.VolunteerClient;
import com.sunday.volunteer.admin.utils.ShowBigImage;
import com.sunday.volunteer.admin.utils.StatusBarUtil;
import com.sunday.volunteer.admin.utils.ViewPagerUtils;

import java.util.Arrays;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*
* 进行中活动详情
* */
public class IndexTab1DetialActivity extends BaseActivity {

    @Bind(R.id.imageView)
    ConvenientBanner viewpager;
    @Bind(R.id.tv_activity_name)
    TextView tvActivityName;
    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.tv_userName)
    TextView tvUserName;
    @Bind(R.id.tv_code)
    TextView tvCode;
    @Bind(R.id.tv_plan_more)
    TextView tvPlanMore;
    @Bind(R.id.tv_detial)
    TextView tvDetial;
    @Bind(R.id.tv_entered)
    TextView tvEntered;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.tv_entry)
    TextView tvEntry;
    @Bind(R.id.img_share)
    ImageView imgShare;
    @Bind(R.id.r_titleView)
    RelativeLayout rTitleView;
    @Bind(R.id.r_nav)
    RelativeLayout rNav;

    private String imgPath;
    private int status ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index_tab1_detial);
        ButterKnife.bind(this);
        StatusBarUtil.setTransparentForImageView(this, null);
        getData();
    }

    private void getData() {
        Call<ResultDO<MyActivities>> call = VolunteerClient.getVolunteerAdapter().getActivityDetail(getIntent().getLongExtra("id", -1),BaseApp.getInstance().getMember().getId());
        call.enqueue(new Callback<ResultDO<MyActivities>>() {
            @Override
            public void onResponse(Call<ResultDO<MyActivities>> call, Response<ResultDO<MyActivities>> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0 && response.body().getResult() != null) {
                    MyActivities activity = response.body().getResult();
                    setData(activity);
                    imgPath = activity.getEwm();
                    status = activity.getStatus();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<MyActivities>> call, Throwable t) {

            }
        });
    }

    private void setData(final MyActivities item) {
        status = item.getStatus();
        if (status == 3) {
            tvEntry.setText("取消活动");
            tvEntry.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDialog(item.getId());
                }
            });
        }else if (status == 5) {
            tvEntry.setText("活动已结束");
        }else if (status == 4) {
            tvEntry.setText("活动进行中");
        }

        new ViewPagerUtils(mContext, viewpager, item.getRollImages());

        tvActivityName.setText(item.getName());
        tvDate.setText(item.getTime());
        tvAddress.setText(item.getAddress());
        tvUserName.setText(item.getLimitNumber()+"人");
        tvDetial.setText(item.getDesc());
        tvEntered.setText(String.format("%d%s%d", item.getJoinNumber(), "/", item.getLimitNumber()));

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        recyclerView.setAdapter(new RecyclerView.Adapter() {

            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(mContext).inflate(R.layout.item_activity_detail, null);
                return new ViewHolder(view);
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                ViewHolder viewHolder = (ViewHolder) holder;
                Picasso.with(mContext).load(item.getMembers().get(position)).resize(80, 80).centerCrop().placeholder(R.mipmap.person_default).into(viewHolder.imageView);
            }

            @Override
            public int getItemCount() {
                return item.getMembers().size();
            }
        });


    }

    //取消活动
    private void showDialog(final long id) {
        final UIAlertView delDialog = new UIAlertView(mContext, "温馨提示", "取消活动？",
                "取消", "确定");
        delDialog.setClicklistener(new UIAlertView.ClickListenerInterface() {
                                       @Override
                                       public void doLeft() {
                                           delDialog.dismiss();
                                       }

                                       @Override
                                       public void doRight() {
                                           cancelAction(id);
                                       }
                                   }
        );
        delDialog.show();
    }

    //取消活动
    private void cancelAction(long id){
        showLoadingDialog(0);
        Call<ResultDO> call = VolunteerClient.getVolunteerAdapter().cancelActitvity(id,BaseApp.getInstance().getMember().getId());
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (response.body() == null||isFinish){
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "活动已取消");
                    EventBus.getDefault().post(new CancelActivityEvent());
                    finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    @OnClick({R.id.tv_code, R.id.tv_plan_more, R.id.img_share})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_code:
                if(imgPath == null){
                    imgPath = "http";
                }
                Intent intent = new Intent(mContext , ShowBigImage.class);
                intent.putExtra("imgPath" , imgPath);
                startActivity(intent);
                break;
            case R.id.tv_plan_more:
                break;
            case R.id.img_share:
                break;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.image_view)
        ImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
